package com.gxa.ehome.web.servlet;

import com.alibaba.fastjson.JSON;
import com.gxa.ehome.util.HttpUtils;
import com.gxa.ehome.util.JsonResult;
import com.gxa.ehome.util.ResponseUtil;
import org.apache.http.HttpResponse;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

// 集中异常的处理方式
@WebServlet(name = "SMServlet",urlPatterns = "/sms")
public class SMServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {

        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setCharacterEncoding("UTF-8");   // 服务器端的字符集设置(影响流)- java代码相关
        servletResponse.setHeader("Content-Type", "text/html;charset=UTF-8"); // 浏览器行为
        Random random = new Random();
        int vcode = random.nextInt(10000);//生成随机4位的验证码
        System.out.println("验证码 = " + vcode);
        JsonResult jsonResult=new JsonResult();
        HttpSession session = servletRequest.getSession();
        session.setAttribute("vcode",vcode+"");

        /*
        String host = "https://gyytz.market.alicloudapi.com";   // 固定不变 , 短信服务商的服务器地址
        String path = "/sms/smsSend";    // 固定不变 , 短信服务商的业务类型
        String method = "POST";   // 固定不变 , 短信服务的请求方式
        String appcode = "2579b610504745e8bc1159850a0c0754";    // 把自己的填进来
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", "15183246772");    // 填写接收短信的手机号
//        querys.put("param", "**code**:12345,**minute**:5");    // 验证码和过期时间(用于提示) , 生成随机数替换12345,还需要记录session
        querys.put("param", "**code**:"+vcode+",**minute**:5");    // 验证码和过期时间(用于提示) , 生成随机数替换12345,还需要记录session
        querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");   // 运营商的固定的签名
        querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad");    //运营商的固定的内容模板
        Map<String, String> bodys = new HashMap<String, String>();




        try {

            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }

         */



        jsonResult.setCode("0");
        jsonResult.setMsg("验证码发送成功");
//        System.out.println(JSON.toJSONString(jsonResult.getValues()));
        ResponseUtil.send(servletResponse, JSON.toJSONString(jsonResult.getValues()));

    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 调用doGet方法, 合二为一
        doPost(request, response);
    }


}
